import { reactive } from 'vue';
import { removeUndefinedParams } from '@/utils/index';
import { getAdTaskList, getFillYearList } from '@/apis/eci/dataManagement/fillInTask';
import type { GetAdTaskListParams } from '@/apis/eci/dataManagement/model/fillInTaskModel';
import { CTableProps } from '@/components/CTable/index';
import { BasicPagingListParams } from '@/apis/basicModel';
import {
  SearchOptionsType,
  ColumnSettingTypeEnum,
  TableFieldsSettingType
} from '@/components/CPageTable';
import { FillInTaskStatusOption } from '@/constants/enum';
import { useRoute } from 'vue-router';

export const useFillInTask = () => {
  const route = useRoute();
  // 搜索字段及其他
  const searchForm = reactive({
    taskName: '',
    status: (route.query?.status as string | undefined) || '',
    dataYear: '',
    updateTime: []
  });
  // 搜索配置
  const searchOptions: SearchOptionsType[] = [
    {
      name: '任务主题',
      field: 'taskName',
      type: 'input'
    },
    {
      name: '任务状态',
      field: 'status',
      type: 'select',
      filterable: true,
      options: FillInTaskStatusOption
    },
    {
      name: '数据年份',
      field: 'dataYear',
      type: 'select',
      filterable: true,
      loadData: (): Promise<any> => {
        return getFillYearList().then(res => {
          const ans = res.map(item => {
            return {
              label: item.dataYear,
              value: item.dataYear
            };
          });
          return ans;
        });
      }
    },
    {
      name: '更新时间',
      field: 'updateTime',
      type: 'datePicker',
      dateType: 'datetimerange',
      startPlaceholder: '更新起始时间',
      endPlaceholder: '更新截止时间',
      format: 'YYYY-MM-DD HH:mm:ss'
    }
  ];
  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  };
  // 表格数据获取
  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter);
    return getAdTaskList(params);
  }
  // 处理参数
  const handleParams = (): GetAdTaskListParams => {
    const [updateTimeStart, updateTimeEnd] = searchForm.updateTime || [];
    // 参数
    let params: GetAdTaskListParams = {
      status: searchForm.status,
      taskName: searchForm.taskName?.trim(),
      dataYear: searchForm.dataYear,
      updateTimeStart,
      updateTimeEnd
    };
    params = removeUndefinedParams(params);
    return params;
  };
  // 表格字段配置
  const tableFieldsSetting: TableFieldsSettingType[] = [
    // {
    //   title: '任务编号',
    //   field: 'taskNo',
    //   width: 230,
    //   fixed: 'left',
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    {
      title: '任务主题/任务编号',
      field: 'taskName',
      slot: 'taskNameSlot',
      minWidth: 300,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '数据周期',
      field: 'fillPeriod',
      minWidth: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '任务状态',
      field: 'status',
      slot: 'statusSlot',
      minWidth: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '下发时间',
      field: 'createTime',
      width: 180,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '更新时间',
      field: 'updateTime',
      width: 180,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 100,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ];

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  };
};
